package com.tablefood.action;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.tablefood.domain.ShopFavorite;
import com.tablefood.domain.ShopProduct;
import com.tablefood.domain.ShopUser;
import com.tablefood.service.FavoriteService;
import com.tablefood.service.ProductService;
import com.tablefood.service.UserService;
import com.tablefood.utils.PageFavBean;
import com.tablefood.utils.PageUserBean;
import com.tablefood.utils.ResultUtils;

public class FavoriteAction extends ActionSupport{

	private ShopUser user ;
	private ShopProduct product ;
	private ShopFavorite favorite ;
	private PageFavBean pageFavBean;
	private List<ShopFavorite> listFav;
	private int page = 1 ;
	@Resource
	private UserService userService;
	@Resource
	private ProductService productService;
	@Resource
	private FavoriteService favoriteService;
	
	public String AddToFav() throws Exception{
		
		// 获取post请求的数据
		Map<String, String> param = new HashMap<String, String>();
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, String[]> params = request.getParameterMap();
		for (String key : params.keySet()) {
			String[] values = params.get(key);
			for (int i = 0; i < values.length; i++) {
				param.put(key, values[i]);

			}
		}
		  
		Map<String, Object> map = new HashMap<String, Object>();
		if (param.get("userid") == "null") {
			map.put("status", -1);
			ResultUtils.toJson(ServletActionContext.getResponse(), map);
			return null;
		}
		
		
		String userid = param.get("userid");//用户编号
		String productid = param.get("productid");//商品编号
		
		user = userService.getUserByUserId(Long.parseLong(userid));
		product = productService.searchProducts(Long.parseLong(productid));
		
		//先查询该商品有没有被收藏
		favorite = favoriteService.searchByProductid(productid);
		if(favorite==null){
			favorite = new ShopFavorite();
			favorite.setShopUser(user);
			favorite.setShopProduct(product);
			favorite.setStatus("1");
			favoriteService.addFavorite(favorite);
			map.put("status", 0);
		}else{
			map.put("status", -1);
		}
		ResultUtils.toJson(ServletActionContext.getResponse(), map);
		return this.SUCCESS;
	}
	
	public String DelToFav() throws Exception{
		
		// 获取post请求的数据
		Map<String, String> param = new HashMap<String, String>();
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, String[]> params = request.getParameterMap();
		for (String key : params.keySet()) {
			String[] values = params.get(key);
			for (int i = 0; i < values.length; i++) {
				param.put(key, values[i]);

			}
		}
		  
		Map<String, Object> map = new HashMap<String, Object>();
		if (param.get("userid") == "null") {
			map.put("status", -1);
			ResultUtils.toJson(ServletActionContext.getResponse(), map);
			return null;
		}
		
		
		String userid = param.get("userid");//用户编号
		String productid = param.get("productid");//商品编号
		
		user = userService.getUserByUserId(Long.parseLong(userid));
		product = productService.searchProducts(Long.parseLong(productid));
		favorite = favoriteService.searchByProductid(productid);
		boolean is = favoriteService.delFavorite(favorite);
		if(is){
			map.put("status", 0);
		}else{
			map.put("status", -1);
		}
		ResultUtils.toJson(ServletActionContext.getResponse(), map);
		return this.SUCCESS;
	}
	
	public String ShowTheFav() throws Exception{
		
		HttpServletRequest request = ServletActionContext.getRequest();
		String userid = request.getParameter("userid");
		this.pageFavBean = favoriteService.queryForPage(userid,5,page);
		this.listFav = this.pageFavBean.getList();
	 
		return this.SUCCESS;
	}
	
	
	
	public ShopUser getUser() {
		return user;
	}
	public void setUser(ShopUser user) {
		this.user = user;
	}
	public ShopProduct getProduct() {
		return product;
	}
	public void setProduct(ShopProduct product) {
		this.product = product;
	}

	public ShopFavorite getFavorite() {
		return favorite;
	}

	public void setFavorite(ShopFavorite favorite) {
		this.favorite = favorite;
	}

	public PageFavBean getPageFavBean() {
		return pageFavBean;
	}

	public void setPageFavBean(PageFavBean pageFavBean) {
		this.pageFavBean = pageFavBean;
	}

	public List<ShopFavorite> getListFav() {
		return listFav;
	}

	public void setListFav(List<ShopFavorite> listFav) {
		this.listFav = listFav;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
	
	
	
}
